package com.example.routeplansystem.controller;

import com.example.routeplansystem.constant.ResponseStatus;
import com.example.routeplansystem.entity.dto.ResponseEntity;
import com.example.routeplansystem.entity.po.VehicleMessage;
import com.example.routeplansystem.service.VehicleService;
import com.example.routeplansystem.service.impl.VehicleServiceImpl;
import com.example.routeplansystem.utils.DateUtil;
import com.example.routeplansystem.utils.validation.insertAndUpdateGroup.Insert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping(value = "vehicleSystem")
@Validated
@Api(tags = "车辆模块")
@Slf4j
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    @PostMapping(value = "/user/vehicle")
    @ApiOperation("通过参数添加车辆")
    public ResponseEntity<String> userVehicleRegister(@Validated(Insert.class) @RequestBody VehicleMessage vehicle, BindingResult result) {

        log.info("------添加车辆信息：" + vehicle + "------" + DateUtil.getFormatDate());

        if (result.hasErrors()) {
            String message = result.getAllErrors().get(0).getDefaultMessage();
            return new ResponseEntity<>(ResponseStatus.FAILURE_RESPONSE, message, null);
        }
        return vehicleService.vehicleRegister(vehicle);
    }
    //添加车辆信息：

    @PostMapping(value = "/excelVehicleInfo/{questionId}")
    @ApiOperation("通过Excel添加车辆")
    public ResponseEntity<String> upload(@NotNull(message = "文件不能为空")
                                 @RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,
                                 @PathVariable("questionId") Integer questionId) {
        return vehicleService.vehicleBatchImport(file, request, questionId);
    }

    //获取车辆信息：
    @GetMapping(value = "/user/vehicle")
    @ApiOperation("获取用户车辆信息 需要登录态")
    @ApiImplicitParam(name = "questionId", value = "问题id")
    public ResponseEntity<List<VehicleMessage>> userVehicleMessage(@RequestParam("questionId") Integer questionId) {
        return vehicleService.userVehicleMessage(questionId);
    }

    //删除车辆信息：
    @DeleteMapping(value = "/user/vehicle")
    @ApiOperation("删除车辆")
    @ApiImplicitParam(name = "vehicleIdList", value = "车辆Id数组 json形式")
    public ResponseEntity<String> userVehicleDelete
            (@NotNull(message = "vehicleIdList 不能为空") @RequestBody List<Integer> vehicleIdList) {

        log.info("------接收到需要删除的车辆ID信息：" + vehicleIdList + "------" + DateUtil.getFormatDate());

        return vehicleService.deleteVehicleBatch(vehicleIdList);
    }

}
